package com.yyge.controller.articleType;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yyge.controller.BaseController;
import com.yyge.controller.articleType.req.ArticleTypeAddReq;
import com.yyge.controller.articleType.req.ArticleTypeEditReq;
import com.yyge.controller.articleType.req.ArticleTypeReq;
import com.yyge.controller.articleType.resp.ArticleTypeResp;
import com.yyge.convertor.ArticleTypeConvertor;
import com.yyge.dto.Query;
import com.yyge.dto.ResultCode;
import com.yyge.entity.ArticleType;
import com.yyge.service.ArticleTypeService;
import com.yyge.dto.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yyge.vo.PageVo;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import javax.crypto.spec.PSource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/articletype")
@Api(value = "文章分类相关接口",tags = "文章分类")
public class ArticleTypeController extends BaseController<ArticleType, ArticleTypeResp, ArticleTypeConvertor> {
    // 注入业务逻辑层
    private ArticleTypeService service;

    @Autowired/*自动装配*/
    public void setService(ArticleTypeService articletypeService){
        this.service=articletypeService;
    }

    @GetMapping/* GET(SELECT): 查询 */
    @ApiOperation(value = "文章分类分页查询",notes = "文章分类分页查询")
    public R<?> get(ArticleTypeReq articleTypeReq, Query query) throws Exception  {
         QueryWrapper<ArticleType> wrapper = this.getWrapper(ArticleTypeConvertor.MAPPER.ob2en(articleTypeReq));
         Page<ArticleType> page = this.query2Page(wrapper, query, (p, w) -> service.page(p, w));
        return R.ok(this.page2PageVo(page,ArticleTypeConvertor.MAPPER::ob2respArr));
    }


    @PostMapping/* POST(CREATE): 新增&登陆 */
    @ApiOperation(value = "文章分类新增",notes = "文章分类新增")
    public R<?> post(@Validated @RequestBody ArticleTypeAddReq articleTypeAddReq){
        ArticleType articleType = ArticleTypeConvertor.MAPPER.ob2en(articleTypeAddReq);
        List<ArticleType> list = service.list(new QueryWrapper<>(articleType));
        if(!list.isEmpty()){
            throw new RuntimeException("当前文章分类已存在");
        }
        articleType.setCreateUser(this.getUser().getId());
        articleType.setUpdateUser(this.getUser().getId());
        boolean i=service.save(articleType);
        return i?R.ok("新增成功！"):R.error(ResultCode.SYSTEM_SERVICE_ADD_ERROR);
    }

    @PutMapping/* POST(CREATE): 更新 */
    @ApiOperation(value = "文章分类修改",notes = "文章分类修改")
    public R<?> edit(@RequestBody ArticleTypeEditReq articleTypeEditReq){
        ArticleType articleType = ArticleTypeConvertor.MAPPER.ob2en(articleTypeEditReq);
        boolean i=service.updateById(articleType);
        return i?R.ok("修改成功！"):R.error(ResultCode.SYSTEM_SERVICE_MODIFY_ERROR);
    }

    @PatchMapping/* PUT(UPDATE): 部分更新 */
    public R patch(@RequestBody ArticleType articletype){
        return null;
    }

    @DeleteMapping("/{id}")/* DELETE(DELETE): 删除&&批量删除 */
    @ApiOperation(value = "文章分类删除",notes = "文章分类删除")
    public R delete(@PathVariable("id") Long[] list){
        boolean i = service.removeByIds(Arrays.asList(list));
        return i?R.ok("修改成功！"):R.error(ResultCode.SYSTEM_SERVICE_DELETE_ERROR);
    }
}


